//
//  BookingEditViewController.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/12/14.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "BookingEditViewController.h"
#import "MBSliderView.h"
#import "BookingConfirmSubjectCell.h"
#import "BookingEditTimeCell.h"
#import "BookingConfirmRoomCell.h"
#import "BookingInfoModel.h"
#import "RoomModel.h"


static NSString *const kBookingConfirmSubjectCellID = @"BookingConfirmSubjectCellID";
static NSString *const kBookingEditTimeCellID    = @"BookingEditTimeCellID";
static NSString *const kBookingConfirmRoomCellID    = @"BookingConfirmRoomCellID";


@interface BookingEditViewController ()<UITableViewDelegate, UITableViewDataSource, MBSliderViewDelegate, BookingEditTimeCellDelegate>

@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, strong) MBSliderView *slider;

//修改后的预定信息
@property (nonatomic, strong) BookingInfoModel *editedBookingInfo;

@end


@implementation BookingEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [[UIColor colorWithHexString:@"#000000"] colorWithAlphaComponent:0.6];
    
    [self initNavBarItem];
    
    //tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    
    UINib *nib = [UINib nibWithNibName:@"BookingConfirmSubjectCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kBookingConfirmSubjectCellID];
    nib = [UINib nibWithNibName:@"BookingEditTimeCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kBookingEditTimeCellID];
    nib = [UINib nibWithNibName:@"BookingConfirmRoomCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kBookingConfirmRoomCellID];
    
    //slider
    self.slider = [[MBSliderView alloc] init];
    [self.slider setText:@"slide to delete!"];
    [self.slider setDelegate:self];
    self.slider.backgroundColor = [UIColor colorWithHexString:@"e2513c"];
    [self.view addSubview:self.slider];
    
    self.editedBookingInfo = [[BookingInfoModel alloc] init];
    self.editedBookingInfo = [self.bookingInfo copy];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.slider makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(20);
        make.right.equalTo(self.view.right).offset(-20);
        make.bottom.equalTo(self.view.bottom).offset(-30);
        make.height.equalTo(@44);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - MBSliderViewDelegate
- (void) sliderDidSlide:(MBSliderView *)slideView {
    [self.delegate viewController:self deleteBooking:YES];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - BookingEditTimeCellDelegate
-(void)tableViewCell:(BookingEditTimeCell *)tbCell extendMeeting:(NSTimeInterval)extendTime{
    self.editedBookingInfo.end = [self.bookingInfo.end dateByAddingTimeInterval:extendTime];
    [self.tableView reloadData];
}

#pragma mark - tableViewDelgate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 || indexPath.row == 2) {
        return 50.f;
    }
    else{
        return 110.f;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 12.f;
    }
    return 0.1f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        BookingConfirmSubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:kBookingConfirmSubjectCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row == 1){
        BookingEditTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:kBookingEditTimeCellID];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"MMM d,hh:mma";
        
        NSString *startStr, *endStr;
        if (self.editedBookingInfo.start != nil) {
            startStr = [dateFormatter stringFromDate:self.editedBookingInfo.start];
        }
        if (self.editedBookingInfo.end != nil) {
            endStr = [dateFormatter stringFromDate:self.editedBookingInfo.end];
        }
        cell.time = [NSString stringWithFormat:@"%@-%@", startStr, endStr];
        NSTimeInterval duration = [self.editedBookingInfo.end timeIntervalSinceDate:self.editedBookingInfo.start];
        
        NSInteger hour = (int)(duration/3600);
        NSInteger minute = (int)((duration - 3600 * hour)/60);
        cell.duration = [NSString stringWithFormat:@"%li Hour %li Min",(long)hour, (long)minute];
        return cell;
    }
    else if (indexPath.row == 2){
        BookingConfirmRoomCell *cell = [tableView dequeueReusableCellWithIdentifier:kBookingConfirmRoomCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.room = [NSString stringWithFormat:@"%@(%iF, %i Persons)", self.editedBookingInfo.roomInfo.name, self.editedBookingInfo.roomInfo.floor, self.editedBookingInfo.roomInfo.capacity];
        return cell;
    }
    return nil;
}

#pragma mark - layout
-(void)initNavBarItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelEdit)];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveEdit)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
}

-(void)cancelEdit {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)saveEdit {
    [self.delegate viewController:self didFinishEditing:self.editedBookingInfo];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

@end
